/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAO.MyConnectUnit;
import DTO.UserDTO;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;
import org.apache.catalina.User;

/**
 *
 * @author MK
 */
public class BUSUser {

    MyConnectUnit connect;

    public BUSUser() {
        this.connect = DAO.DAO.getDAO();
    }

    //Lấy tất cả đối tượng trong CSDL
    public HashSet<UserDTO> getUsers(String condition, String OrderBy) throws Exception {
        ResultSet result = this.connect.Select("User", condition, OrderBy);
        HashSet<UserDTO> users = new HashSet<UserDTO>();
        while (result.next()) {
            UserDTO user = new UserDTO();
            user.setUsername(result.getString("Username"));
            user.setPassword(result.getString("Password"));
            users.add(user);
        }
        return users;
    }

    public HashSet<UserDTO> getUsers(String condition) throws Exception {
        return getUsers(condition, null);
    }

    public HashSet<UserDTO> getUsers() throws Exception {
        return getUsers(null);
    }

    //Lấy CSDL thông qua khóa chính
    public User getByID(int id) throws Exception {
        HashSet<UserDTO> users = this.getUsers("Username = " + id);
        if (users.size() > 0) {
            return (User) users.toArray(new UserDTO[users.size()])[0];
        }
        return null;
    }

    public void Insert(UserDTO user) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("Username", user.getUsername());
        map.put("Password", user.getPassword());
        map.put("Email", user.getEmail());
        map.put("Phone", user.getPhone());
        map.put("Fullname", user.getFullname());
        this.connect.Insert("User", map);
    }

    //Insert 1 danh sách
    public void Inserts(HashSet<UserDTO> users) throws Exception {
        for (UserDTO user : users) {
            this.Insert(user);
        }
    }

    public void Update(UserDTO user) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("Password", user.getPassword());
        map.put("Phone", user.getPhone());
        map.put("Fullname", user.getFullname());
        this.connect.Update("User", map, "Password=" + user.getPassword() + "Phone=" + user.getPhone() + "Fullname=" + user.getFullname());
    }

    public void Updates(HashSet<UserDTO> users) throws Exception {
        for (UserDTO user : users) {
            this.Update(user);
        }
    }

    public void Delete(UserDTO user) throws Exception {
        this.connect.Delete("User", "Username=" + user.getUsername());
    }

    public void Deletes(HashSet<UserDTO> users) throws Exception {
        for (UserDTO user : users) {
            this.Update(user);
        }
    }
}
